<?php

/**
 * This is the model class for table "adminhtml_Role".
 *
 * The followings are the available columns in table 'adminhtml_Role':
 * @property integer $role_id
 * @property string $role_name
 * @property string $role_label
 * @property string $acl_desc
 * @property integer $p_id
 * @property string $create_time
 * @property string $update_time
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property AdminhtmlAdmin[] $adminhtmlAdmins
 */
class AdminhtmlRole extends AdminhtmlCAR
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return AdminhtmlRole the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getResourceName()
	{
		return '管理员角色';
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'adminhtml_role';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('p_id, status', 'numerical', 'integerOnly'=>true),
			array('role_name, role_label', 'length', 'max'=>20),
			array('role_name', 'unique'),
			array('acl_desc, create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('role_id, role_name, role_label, acl_desc, p_id, create_time, update_time, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'adminhtmlAdmins' => array(self::HAS_MANY, 'AdminhtmlAdmin', 'role_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'role_id' => 'ID',
			'role_name' => '角色标识',
			'role_label' => '角色名称',
			'acl_desc' => 'ACL序列化',
			'p_id' => '上级ACL',
			'create_time' => '创建时间',
			'update_time' => '更新时间',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('role_name',$this->role_name,true);
		$criteria->compare('role_label',$this->role_label,true);
		$criteria->compare('acl_desc',$this->acl_desc,true);
		$criteria->compare('p_id',$this->p_id);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'sort'=>array('defaultOrder'=>'role_id DESC'),
			'pagination'=>array('pageSize'=>20),
		));
	}

	public function toOptionHash($filter= array(), $key='role_id', $value='role_label' )
	{
		$return= array();
		$result= $this->findAllByAttributes((array) $filter);
		for($i=0; $i<count($result); $i++) {
			$return[$result[$i][$key] ] = $result[$i][$value];
		}
		return $return;
	}
	
	public function getAclArrays()
	{
		$array = array();
		foreach ($this->findAll() as $v) {
			$array[$v->getAttribute('role_id')]= unserialize($v->getAttribute('acl_desc'));
		}
		return $array;
	}
	
	public function getOneAclArray($roleId=null, $filter= array() )
	{
		if ($roleId==null) {
			throw new CHttpException(404,'RoleId can not be empty!');
			
		} else {
			$filter= array_merge($filter, array('role_id'=> $roleId));
			$result= $this->findByAttributes($filter);
			if($result->getAttribute('acl_desc')=== Yii::app()->params['fullAccess'] ) {
				return Yii::app()->params['fullAccess'];
			} else {
				return unserialize( $result->getAttribute('acl_desc'));
			}
		}
	}
	
	public function getRoleTreeArray($level=0 )
	{
		$tree = array();
		$controllers= AdminhtmlHelper::getControllerOptions(TRUE);
		foreach ($controllers as $k=> $v) {
			$ctl_path= dirname(dirname(__FILE__)). '/controllers/';
			//echo $ctl_path;die;
			require_once($ctl_path. $v. '.php');
			$ctlObj= @new $v(substr($v,0,-10) );
			$ctlObj->init();
			$tree[$v]['label'] = $ctlObj->controllerLabel;
			$actions = get_class_methods($v);
			foreach ($actions as $sv) {
				if ($sv=='actions') {
					continue;
				}
				if ( preg_match('/^action.*/i', $sv) ) {
					$tree[$v]['actions'][]= strtolower(substr($sv, 6));
				}
			}
		}
		return $tree;
	}
	
	public function formArrayToAclDesc($formArray=array())
	{
		$return = array();
		foreach ($formArray as $k=> $v) {
			list($controller, $action) = explode('__', $k);
			$return[$controller][] = $action;
		}
		return serialize($return);
	}
}